package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.contract.LaunchContract;
import com.rdc.qingkou.presenter.LaunchPresenter;
import com.rdc.qingkou.utils.AppUtil;
import com.rdc.qingkou.utils.DirectedLoginUtil;
import com.rdc.qingkou.utils.MyUtils;

public class LaunchActivity extends BaseActivity<LaunchPresenter> implements LaunchContract.View {
    private Button mLogin;
    private Button mRegister;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        showActivity();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        showActivity();
    }

    private void showActivity(){
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);

        if (DirectedLoginUtil.checkIsLogined() == true) {
            if (AppUtil.isAppInForeground(getApplicationContext())){
                setContentView(R.layout.launch_login);
            }

            if (!MyUtils.isNetworkConnected(getApplicationContext())){
                Toast.makeText(this,"网络好像有点问题！",Toast.LENGTH_SHORT).show();
                jumpToLogin();
                finish();
            }else {
                mPresenter.jumpToWelcome();
                mPresenter.setWelcomed();
            }
            MyApplication.addActivity(this);
        } else {
            setContentView(R.layout.launch);
            init();
        }
    }


    private void init() {
        mLogin = (Button) findViewById(R.id.btn_login);
        mRegister = (Button) findViewById(R.id.btn_register);


        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.jumpToLogin();
            }
        });


        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.jumpToRegister();
            }
        });

        mPresenter.jumpToWelcome();
        mPresenter.setWelcomed();

        MyApplication.addActivity(this);
    }


    @Override
    public void jumpToLogin() {
        Intent intent = new Intent(LaunchActivity.this, LoginActivity.class);
        startActivity(intent);

    }

    @Override
    public void jumpToRegister() {
        Intent intent = new Intent(LaunchActivity.this, RegisterActivity.class);
        startActivity(intent);

    }

    @Override
    public void jumpToWelcome() {
        Intent intent = new Intent(LaunchActivity.this, WelcomeActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public LaunchPresenter getInstance() {
        return new LaunchPresenter();
    }


    @Override
    public void jumpToHomepage() {
        Intent intent = new Intent(LaunchActivity.this, HomepageActivity.class);
        startActivity(intent);
        finish();
    }
}